# -*- coding: UTF-8 -*- 
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.db import models

class CategoryPublishManager(models.Manager):
    def get_query_set(self):
        queryset = super(CategoryPublishManager, self).get_query_set()
        return queryset.filter(status=0)

class BaseProductPublishManager(models.Manager):
    def get_query_set(self):
        queryset = super(BaseProductPublishManager, self).get_query_set()
        return queryset.filter(status=0)
         
class Category(models.Model):
    STATUS_CHOICES = ((0, _(u"启用")),
                      (1, _(u"禁用")),)
    name = models.CharField(_(u"名称"),max_length=200)
    slug = models.CharField(_(u"缩略名"),max_length=200)
    sort_index = models.IntegerField(_(u"排序"))
    status = models.IntegerField(_(u"状态"),choices=STATUS_CHOICES,default=0)
    
    def __unicode__(self):
        return self.name
    
    publish = CategoryPublishManager()

    class Meta:
        verbose_name = _(u"分类")
        verbose_name_plural = _(u"分类")
        
    @models.permalink
    def get_absolute_url(self):
        return ('card_category',None,{'id':self.id})
    
class BaseProduct(models.Model):
    STATUS_CHOICES = ((0, _(u"启用")),
                      (1, _(u"禁用")),)
    name = models.CharField(_(u"名称"),max_length=200)
    category = models.ForeignKey(Category,verbose_name=_(u"所属分类"))
    price = models.FloatField(_(u"价格"))
    describle = models.TextField(_(u"产品描述"))
    sort_index = models.IntegerField(_(u"排序"))
    status = models.IntegerField(_(u"状态"),choices=STATUS_CHOICES,default=0)
    
    def __unicode__(self):
        return self.name
    
    objects = models.Manager()
    publish = BaseProductPublishManager()
    
    class Meta:
        abstract = True
        
    @models.permalink
    def get_absolute_url(self):
        return ('card_detail',None,{'id':self.id})
        
class Card(BaseProduct):
    img = models.ImageField(upload_to="upload/card/",verbose_name=_(u"图片"))
    class Meta:
        verbose_name = _(u"充值卡")
        verbose_name_plural = _(u"充值卡")
                
class BaseOrder(models.Model):
    STATUS_CHOICES = ((0, _(u"未付款")),
                      (1, _(u"待发货")),
                      (2, _(u"已完成")),
                      (3, _(u"已取消")),)
    code = models.CharField(_(u"订单编号"),max_length=200)
    product_name = models.CharField(_(u"产品名称"),max_length=200)
    product_quantity = models.IntegerField(_(u"购买数量"))
    product_price = models.FloatField(_(u"产品价格"))
    total_amount = models.FloatField(_(u"总金额"))
    user = models.ForeignKey(User,verbose_name=_(u"用户"))
    create_on = models.DateTimeField(_(u"下单时间"),auto_now_add=True)
    create_ip = models.IPAddressField(_(u"下单地址"))
    status = models.IntegerField(_(u"订单状态"),choices=STATUS_CHOICES,default=0)
    class Meta:
        abstract = True
        
    def __unicode__(self):
        return self.code
        
class CardOrderManager(models.Manager):
    def create_order(self,card,quantity,user,ip):
        import time
        order = self.create(card=card,
                            user=user,
                            product_name=card.name,
                            product_price=card.price,
                            product_quantity=quantity,
                            total_amount=float(quantity) * float(card.price),
                            create_ip=ip)
        order.code = '%s%s' % (time.strftime('%y%m%d%H%M%S'),order.id)
        order.save()
        order.delivery()
        return order        
        
class CardOrder(BaseOrder):
    card = models.ForeignKey(Card,verbose_name=_(u"充值卡"))
    objects = CardOrderManager()
    
    def delivery(self):
        card_numbers = CardNumber.objects.filter(status=0,card=self.card)[:self.product_quantity]
        for card_number in card_numbers:
            card_number.order = self
            card_number.status = 1
            card_number.save()
            
    class Meta:
        verbose_name = _(u"订单")
        verbose_name_plural = _(u"订单")
        
class CardNumber(models.Model):
    STATUS_CHOICES = ((0, _(u"正常")),
                      (1, _(u"售出")),)
    number = models.CharField(_(u"卡号"),max_length=20)
    card = models.ForeignKey(Card,verbose_name=_(u"所属充值卡"))
    order = models.ForeignKey(CardOrder,verbose_name=_(u"订单"),null=True, blank=True)
    create_on = models.DateTimeField(_(u"创建时间"),auto_now_add=True)
    create_by = models.ForeignKey(User,verbose_name=_(u"用户"),null=True, blank=True)
    status = models.IntegerField(_(u"状态"),choices=STATUS_CHOICES,default=0)
    
    def __unicode__(self):
        return self.number
    
    class Meta:
        verbose_name = _(u"卡号")
        verbose_name_plural = _(u"卡号")
    
    